package com.ruoyi.st.service.impl;

import com.ruoyi.common.config.response.StatusCode;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.st.domain.StUser;
import com.ruoyi.st.domain.param.UserLoginParam;
import com.ruoyi.st.domain.vo.UserVo;
import com.ruoyi.st.mapper.StUserMapper;
import com.ruoyi.st.service.IStUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-07
 */
@Slf4j
@Service
public class StUserServiceImpl implements IStUserService 
{
    @Autowired
    private StUserMapper stUserMapper;

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public StUser selectStUserById(Long id)
    {
        return stUserMapper.selectStUserById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param stUser 用户
     * @return 用户
     */
    @Override
    public List<StUser> selectStUserList(StUser stUser)
    {
        return stUserMapper.selectStUserList(stUser);
    }

    /**
     *
     * @param userLoginParam
     * @return
     */
    @Override
    public UserVo selectStUserByUserParam(UserLoginParam userLoginParam) {
        log.debug("用户登录，参数：{}",userLoginParam);
        UserVo userVo = stUserMapper.selectByUsername(userLoginParam.getUsername());
        log.debug("返回对象，参数：{}",userVo.toString());
        if(userVo==null){
            //主业务如果遇到了问题,需要抛出业务层异常,而不是直接给前端返回结果
            //Controller层才是给前端返回结果的那一层
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVo.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVo;
    }


    /**
     * 新增用户
     * 
     * @param stUser 用户
     * @return 结果
     */
    @Override
    public int insertStUser(StUser stUser)
    {
        return stUserMapper.insertStUser(stUser);
    }

    /**
     * 修改用户
     * 
     * @param stUser 用户
     * @return 结果
     */
    @Override
    public int updateStUser(StUser stUser)
    {
        return stUserMapper.updateStUser(stUser);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteStUserByIds(Long[] ids)
    {
        return stUserMapper.deleteStUserByIds(ids);
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteStUserById(Long id)
    {
        return stUserMapper.deleteStUserById(id);
    }
}
